﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tenebrous_Manor.Randomization
{
    static class RandomizedFurniture
    {
        static public Dictionary<Navigation.Room, Furniture[]> random_furniture = new Dictionary<Navigation.Room, Furniture[]>();
        static Random randomNumbers = new Random((int)DateTime.Now.Ticks);

        static public void setRandomFurniture(ref int[,] map, Navigation.Room room)
        {
            //Don't generate random furniture for these rooms
            if (room == Navigation.Room.MAIN_ENTRANCE
                ||room == Navigation.Room.JUNK_ROOM 
                || room == Navigation.Room.KITCHEN
                || room == Navigation.Room.LOWER_LANDING
                //|| room == Navigation.Room.BASEMENT
                || room == Navigation.Room.RESEARCH_LAB)
                return;

            if (!random_furniture.ContainsKey(room))
            {
                int x, y;
                Furniture[] temp = new Furniture[GameConstants.NUM_RAND_FURNITURE];
                for (int i = 0; i <= temp.Length - 1; i++)
                {
                    do{
                        x = randomNumbers.Next(1, GameConstants.COL_COUNT -2);
                        y = randomNumbers.Next(1, GameConstants.ROW_COUNT -2);
                    }while(map[x,y] != 0 || isNextToDoor(ref map, x, y));

                    temp[i] = new Furniture(x, y);
                }

                random_furniture.Add(room, temp);
            }

            foreach (Furniture furniture in random_furniture[room])
            {
                map[furniture.coordinates.Item1, furniture.coordinates.Item2] = 1;
            }
        }

        static private bool isNextToDoor(ref int[,] map, int x, int y)
        {
            if (map[x - 1, y] == 2
                || map[x + 1, y] == 2
                || map[x, y - 1] == 2
                || map[x, y + 1] == 2
             )
                return true;

            return false;
        }

    }
}
